package com.wunu.controller;

import com.alibaba.fastjson.JSONObject;
import com.wunu.annotation.CurrentUser;
import com.wunu.annotation.Log;
import com.wunu.annotation.Pass;
import com.wunu.annotation.ValidationParam;
import com.wunu.base.BusinessException;
import com.wunu.base.PublicResultConstant;
import com.wunu.config.ResponseHelper;
import com.wunu.config.ResponseModel;
import com.wunu.entity.User;
import com.wunu.service.IUserService;
import com.wunu.util.ComUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 *  登录接口
 * @author wunu kin
 * @since 2019-03-25
 */
@CrossOrigin
@RestController
@Api(description="身份认证模块")
public class LoginController {
    @Autowired
    private IUserService userService;


    @ApiOperation(value="用户密码登录", notes="body体参数,不需要Authorization",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "requestJson", value = "{\"userName\":\"admin\",\"password\":\"123456\"}"
                    , required = true, dataType = "String",paramType="body")
    })
    @PostMapping("/login")
    @Log(action="SignIn",modelName= "Login",description="前台密码登录接口")
    @Pass
    public ResponseModel<Map<String, Object>> login(
			 @ValidationParam("userName,password") @RequestBody JSONObject requestJson) throws Exception{
        return ResponseHelper.buildResponseModel(userService.checkAccountAndPasswd(requestJson));
    }

    @ApiOperation(value="短信验证码登录", notes="body体参数,不需要Authorization",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "requestJson", value = "{\"mobile\":\"13888888888\",\"captcha\":\"5780\"}"
                    , required = true, dataType = "String",paramType="body")
    })
    @PostMapping("/login/captcha")
    @Log(action="SignInByCaptcha",modelName= "Login",description="前台短信验证码登录接口")
    @Pass
    @ApiIgnore
    public ResponseModel<Map<String, Object>> loginBycaptcha(
            @ValidationParam("mobile,captcha")@RequestBody JSONObject requestJson) throws Exception{
        return ResponseHelper.buildResponseModel( userService.checkMobileAndCatcha(requestJson));
    }



    @ApiOperation(value="后台用户注册", notes="body体参数,不需要Authorization",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "requestJson", value = "{\"userName\":\"wunu\",</br>\"phone\":\"13888888888\",</br>"
            		+ "\"name\":\"悟牛科技\",</br>" +
                    "\"password\":\"123456\",</br>\"repassword\":\"123456\",</br>\"roleCode\":\"role-cf8fea2055344df59a0d3e80540c78f9\",</br>\"status\":\"1\",</br>}"
                    , required = true, dataType = "String",paramType="body")
    })
    @PostMapping("/register")
    @Log(action="register",modelName= "Login",description="注册接口")
    public ResponseModel<User> register(@CurrentUser User currentUser ,
    									@ValidationParam("userName,name,roleCode,password,repassword,phone,status")
                                        @RequestBody JSONObject requestJson)throws Exception {
        return ResponseHelper.buildResponseModel( userService.checkAndRegisterUser(currentUser ,requestJson));
    }


    @ApiOperation(value="忘记密码", notes="body体参数,不需要Authorization",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "requestJson", value = "{\"mobile\":\"17765071662\",\"captcha\":\"5780\",</br>" +
                    "\"password\":\"123456\",\"rePassword\":\"123456\"}"
                    , required = true, dataType = "String",paramType="body")
    })
    @PostMapping("/forget/password")
    @Pass
    @ApiIgnore
    public ResponseModel<User> resetPassWord (@ValidationParam("mobile,captcha,password,rePassword")
                                               @RequestBody JSONObject requestJson ) throws Exception{
        return ResponseHelper.buildResponseModel(userService.updateForgetPasswd(requestJson));
    }

    /**
     * 检查用户是否注册过
     * @param mobile
     * @return
     * @throws Exception
     */
    @ApiOperation(value="检测用户手机是否注册", notes="根据url的phone/userName来查询用户")
    @ApiImplicitParam(name = "phone", value = "13800138000", required = true, dataType = "String",paramType = "query") 
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "13800138000"
                , dataType = "String",paramType="query"),
        @ApiImplicitParam(name = "userName", value = "iwunu"
                , dataType = "String",paramType="query")
        })
    @GetMapping("/check/user")
    @Pass
    public ResponseModel loginBycaptcha(@RequestParam("phone") String phone,@RequestParam("userName") String userName) throws Exception{
        User user = userService.getUserByMobile(phone);
        User user1 = userService.getUserByUserName(userName);
        if(!ComUtil.isEmpty(user)) {
        	throw new BusinessException(PublicResultConstant.EXIT_USER_PHONE);
        }else if(!ComUtil.isEmpty(user1))  {
        	throw new BusinessException(PublicResultConstant.EXIT_USER);
        }
        return ResponseHelper.buildResponseModel(true);
    }
    
    /**
     * 检查用户是否注册过
     * @param mobile
     * @return
     * @throws Exception
     */
    @ApiOperation(value="检测用户名是否注册", notes="根据url的userName来查询用户")
    @ApiImplicitParam(name = "userName", value = "iwunu", required = true, dataType = "String",paramType = "query") 
    @GetMapping("/check/userName")
    @Pass
    @ApiIgnore
    public ResponseModel loginByUserName(@RequestParam("userName") String userName) throws Exception{
        User user = userService.getUserByUserName(userName);
        return ResponseHelper.buildResponseModel(!ComUtil.isEmpty(user));
    }
}
